package cn.yeyiu.netonlinevideo.util;

import android.content.Context;

import androidx.appcompat.app.AlertDialog;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import cn.yeyiu.mangaviewer.MangaDetailActivity;
import cn.yeyiu.netonlinevideo.R;
import cn.yeyiu.netonlinevideo.activity.BlogViewActivity;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.BlogInfo;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.util.AndroidUtil;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.ThreadUtil;
import cn.yeyiu.util.bean.MangaDetail;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoDetail;
import cn.yeyiu.util.bean.VideoInfo;
import cn.yeyiu.videoplayer.VideoDetailActivity;
import cn.yeyiu.videoplayer.VideoPlayerActivity;

public class TargetUtil {
    /**
     * 根据情况 去往不同activity
     *
     * @param mContext
     * @param mediaData
     */
    public static void go(Context mContext, MediaData mediaData) {
        AlertDialog dialog = new AlertDialog.Builder(mContext)
                .setTitle("获取元数据")
                .setView(R.layout.item_dialog_waiting)
                .create();
        dialog.show();

        ThreadUtil.run(() -> {
            Parser parser = ParserFactory.getParser(mediaData.code);
            if (parser == null) {
                AndroidUtil.toast("非法类型，无法解析：" + mediaData.code);

            } else if ("video".equals(parser.source.type)) {

                if (parser.source.serial) {
                    VideoDetail videoDetail = (VideoDetail) parser.buildData(mediaData);
                    if (videoDetail == null) {
                        AndroidUtil.toast("获取地址失败，更新代码！");
                    } else {
                        MediaUtil.viewVideoDetail(mContext, VideoDetailActivity.class, videoDetail);
                    }
                } else {
                    VideoInfo videoInfo = (VideoInfo) parser.buildData(mediaData);
                    if (videoInfo == null || "".equals(videoInfo.videoSrc)) {
                        AndroidUtil.toast("获取地址失败，可能到达观看次数，明天再试！");
                    } else {
                        MediaUtil.playVideo(mContext, VideoPlayerActivity.class, videoInfo);
                    }
                }

            } else if ("manga".equals(parser.source.type)) {

                if (parser.source.serial) {
                    MangaDetail mangaDetail = (MangaDetail) parser.buildData(mediaData);
                    if (mangaDetail == null) {
                        AndroidUtil.toast("获取地址失败，可能到达阅读次数，明天再试！");
                    } else {
                        MediaUtil.readManga(mContext, MangaDetailActivity.class, mangaDetail);
                    }
                } else {
                    System.out.println("未实现功能，检查！");
                }

            } else if ("blog".equals(parser.source.type)) {
                BlogInfo blogInfo = (BlogInfo) parser.buildData(mediaData);
                if (blogInfo == null) {
                    AndroidUtil.toast("获取地址失败，网络问题");
                } else {
                    MediaUtil.readBlog(mContext, BlogViewActivity.class, blogInfo);
                }
            }
            dialog.dismiss();
        });
    }
}
